package com.tencent.qcloud.tim.demo.chat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.tencent.imsdk.ext.message.TIMMessageReceipt;
import com.tencent.qcloud.tim.demo.R;
import com.tencent.qcloud.tim.demo.SplashActivity;
import com.tencent.qcloud.tim.demo.utils.Constants;
import com.tencent.qcloud.tim.uikit.TUIKit;
import com.tencent.qcloud.tim.uikit.base.MessageReceiptListener;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.tencent.qcloud.tim.uikit.utils.TUIKitLog;

import java.util.List;

public class ChatActivity extends Activity {

    private static String TAG = ChatActivity.class.getSimpleName();

    private ChatFragment mChatFragment;
    private ChatInfo mChatInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_activity);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null) {
            startSplashActivity();
        } else {
            mChatInfo = (ChatInfo) bundle.getSerializable(Constants.CHAT_INFO);
            if (mChatInfo == null) {
                startSplashActivity();
                return;
            }
            mChatFragment = new ChatFragment();
            mChatFragment.setArguments(bundle);
            getFragmentManager().beginTransaction().replace(R.id.empty_view, mChatFragment).commitAllowingStateLoss();
        }

        // 设置回执监听器
        TUIKit.setMessageReceiptListener(new MessageReceiptListener() {
            @Override
            public void onMessageReceipt(List<TIMMessageReceipt> list) {
                // 设置会话拉取最新消息数据刷新 UI
                TUIKitLog.e(TAG , "loadMessages 1");
                mChatFragment.loadMessages();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        TUIKit.setMessageReceiptListener(null);
    }

    private void startSplashActivity() {
        Intent intent = new Intent(ChatActivity.this, SplashActivity.class);
        startActivity(intent);
        finish();
    }
}
